package com.tedu.udm.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.tedu.udm.pojo.dto.UserAddNewDTO;
import com.tedu.udm.pojo.dto.UserLoginDTO;
import com.tedu.udm.pojo.dto.UserUpdateClassDTO;
import com.tedu.udm.pojo.vo.UserListTeacherVO;
import com.tedu.udm.pojo.vo.UserListTeamVO;
import com.tedu.udm.pojo.vo.UserLoginInfoVO;
import com.tedu.udm.pojo.vo.UserListItemVO;
import com.tedu.udm.service.IUserService;
import com.tedu.udm.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api(tags = "01. 用户管理模块")
@Validated
@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    IUserService userService;

    public UserController() {
        log.info("创建控制器对象：UserController");
    }

    // http://localhost:9878/users/login?username=aa&phone=bb&email=cc
    @ApiOperation("用户登录")
    @ApiOperationSupport(order = 50)
    @PostMapping("/login")
    public JsonResult<String> login(UserLoginDTO userLoginDTO){
        log.debug("开始处理【用户登录】的请求，参数：{}", userLoginDTO);
        String jwt = userService.login(userLoginDTO);
        return JsonResult.ok(jwt);
    }

    // http://localhost:9878/users/add-new?username=aa&phone=bb&email=cc
    @ApiOperation("添加用户")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(UserAddNewDTO userAddNewDTO) {
        log.debug("开始处理【添加用户】的请求，参数：{}", userAddNewDTO);
        userService.addNew(userAddNewDTO);
        return JsonResult.ok();
    }

    // http://localhost:9878/users/9527/delete
    @ApiOperation("根据id删除用户")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> delete(@Range(min = 1, message = "删除用户失败，尝试删除的用户的ID无效！")
                                   @PathVariable Long id) {
        log.debug("开始处理【根据id删除用户】的请求，参数：{}", id);
        userService.delete(id);
        return JsonResult.ok();
    }

    // http://localhost:9878/users/update-class
    @ApiOperation("修改用户班级")
    @ApiOperationSupport(order = 301)
    @PostMapping("/{id:[0-9]+}/update-class")
    public JsonResult<Void> updateClass(@Range(min = 1, message = "删除用户失败，尝试删除的用户的ID无效！")
                                        @PathVariable Long id, UserUpdateClassDTO userUpdateClassDTO) {
        log.debug("开始处理【修改用户班级】的请求，参数：{}", userUpdateClassDTO);
        userService.updateClass(id,userUpdateClassDTO);
        return JsonResult.ok();
    }

    // http://localhost:9878/users/9527/enable
    @ApiOperation("启用用户")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/enable")
    public JsonResult<Void> setEnable(@Range(min = 1, message = "启用用户失败，尝试启用的用户的ID无效！")
                                   @PathVariable Long id) {
        log.debug("开始处理【启用用户】的请求，参数：{}", id);
        userService.setEnable(id);
        return JsonResult.ok();
    }

    // http://localhost:9878/users/9527/disable
    @ApiOperation("禁用用户")
    @ApiOperationSupport(order = 311)
    @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/disable")
    public JsonResult<Void> setDisable(@Range(min = 1, message = "禁用用户失败，尝试禁用的用户的ID无效！")
                                      @PathVariable Long id) {
        System.out.println(id);
        log.debug("开始处理【禁用用户】的请求，参数：{}", id);
        userService.setDisable(id);
        return JsonResult.ok();
    }


    // http://localhost:9878/users
    @ApiOperation("查询用户列表")
    @ApiOperationSupport(order = 400)
    @GetMapping("")
    public JsonResult<List<UserListItemVO>> list() {
        log.debug("开始处理【查询用户列表】的请求，无参数");
        List<UserListItemVO> list = userService.list();
        return JsonResult.ok(list);
    }

    // http://localhost:9878/users/{id}/list"
    @ApiOperation("查询班级学员")
    @ApiOperationSupport(order = 410)
    @GetMapping("/{id:[0-9]+}/list-by-class")
    public JsonResult<List<UserListItemVO>> listByClassId(@PathVariable Long id) {
        log.debug("开始处理【查询班级学员】的请求，无参数");
        List<UserListItemVO> list = userService.listByClassId(id);
        return JsonResult.ok(list);
    }

    // http://localhost:9878/users/{id}/list"
    @ApiOperation("查询班级未分组学员")
    @ApiOperationSupport(order = 411)
    @GetMapping("/{id:[0-9]+}/list-by-class-No-team")
    public JsonResult<List<UserListItemVO>> listByClassIdNoTeam(@PathVariable Long id) {
        log.debug("开始处理【查询班级未分组学员】的请求，无参数");
        List<UserListItemVO> list = userService.listByClassIdNoTeam(id);
        return JsonResult.ok(list);
    }

    // http://localhost:9878/users/{id}/list"
    @ApiOperation("搜索查询学员")
    @ApiOperationSupport(order = 412)
    @GetMapping("/{search}/list-by-search")
    public JsonResult<List<UserListItemVO>> listBySearch(@PathVariable String search) {
        log.debug("开始处理【搜索查询学员】的请求，无参数");
        List<UserListItemVO> list = userService.listBySearch(search);
        return JsonResult.ok(list);
    }

    // http://localhost:9878/users/{id}/list"
    @ApiOperation("查询班级学员")
    @ApiOperationSupport(order = 420)
    @GetMapping("/list-by-teacher")
    public JsonResult<List<UserListTeacherVO>> listByIsTeacher() {
        log.debug("开始处理【查询老师】的请求，无参数");
        List<UserListTeacherVO> list = userService.listByTeacher();
        return JsonResult.ok(list);
    }

    // http://localhost:9878/users/{id}/list"
    @ApiOperation("查询小组学员")
    @ApiOperationSupport(order = 430)
    @GetMapping("/{id:[0-9]+}/list-by-team")
    public JsonResult<List<UserListTeamVO>> listByTeamId(@PathVariable Long id) {
        log.debug("开始处理【查询小组学员】的请求，无参数");
        List<UserListTeamVO> list = userService.listByTeamId(id);
        return JsonResult.ok(list);
    }

}